<?php
namespace app\common\logic;

use app\common\exception\FailException;
use think\Controller;
use app\common\model\Praise as PraiseModel;
use think\Db;
use app\common\model\Picture as PictureModel;
use app\common\logic\Oss;
use think\Exception;

class Praise extends Controller
{

    /**
     * @param $shopid
     * @param int $start
     * @param int $num
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取店铺的点赞列表
     */
    public function getPraiseListByShopid($shopid,$start=0,$num=20){

        return PraiseModel::where("shopid",$shopid)->order("addtime desc")->page($start,$num)->select();

    }

    /**
     * @param $userid
     * @param int $start
     * @param int $num
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户的点赞列表
     */
    public function getPraiseListByUserid($userid,$start=0,$num=20){

        return PraiseModel::where("uid",$userid)->order("addtime desc")->page($start,$num)->select();

    }

    /**
     * @param $shopid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取店铺的点赞数量
     */
    public function getPraiseListByShopidCount($shopid){

        return PraiseModel::where("shopid",$shopid)->count();

    }


    /**
     * @param int $userid
     * @param int $shopid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 评价
     */
    public function add_praise($order_id,$uid,$content="",$star=0,$praise_picture=''){

           $evaluate=\app\common\model\Order::where("id",$order_id)->value("evaluate");
           if($evaluate==2){
               throw new FailException(["code"=>201,"msg"=>"该订单已被评价","errorCode"=>303]);
           }
            $pic_string='';
           if($praise_picture){
               $praise_picture=json_decode($praise_picture,true);
               //TODO

               foreach ($praise_picture as $picture){

                   if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $picture, $result)){
                       $type = $result[2];
                       $key = time() . rand(10000, 99999999).'.'.$type;
                       $new_file = $key;
                       if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
                           if(file_put_contents($new_file, base64_decode(str_replace($result[1], ' ', $picture)))){
                               $results = Oss::publicUpload("zdtx023",'pingjia/'.$key, $new_file);
                               if($results){
                                   $pic_string.= ','.Db::table("t_picture")->insertGetId(["picurl"=>'pingjia/'.$key,"fileName"=>$key,"addtime"=>time(),"updated_at"=>date("Y-m-d H:i:s",time()),"created_at"=>date("Y-m-d H:i:s",time()),"by_uid"=>$uid]);
                                  $pic_string=ltrim($pic_string,",");
                                  unlink($new_file);
                               }
                           }

                       }
                   }
               }
           //    Db::table("t_picture")->insertAll($pic_data);


           }

           Db::startTrans();
           try{

               $order_data=Db::table("t_order")->where("id",$order_id)->where("user_id",$uid)->lock(true)->find();
               if(!$order_data){
                   throw new FailException(['code' => 201,"msg"=>"暂无订单被评论","errorCode"=>404]);
               }
               if($order_data['evaluate']==2){
                   throw new FailException(['code' => 201,"msg"=>"该订单已经被评论","errorCode"=>404]);
               }

               Db::table("t_praise")->insertGetId([
                    'shopid'=>$order_data['shop_id'],
                    'uid'=>$uid,
                    'orderid'=>$order_id,
                    'content'=>$content,
                    'star'=>$star,
                    'picture_id'=>$pic_string,
                    'addtime'=>time()
               ]);
               Db::table("t_order")->where("id",$order_id)->where("user_id",$uid)->update([
                   'evaluate'=>2
               ]);


               Db::commit();
           }catch (FailException $e){
               Db::rollback();
               throw new FailException(['code' => 201,"msg"=>"非法操作","errorCode"=>404]);
           }

           return true;



    }


    /**
     * @param $shopid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺评价
     */
    public function get_user_praise($uid,$start=1,$num=20){

        $praiseinfo=\app\common\model\Praise::alias('p')
            ->join('t_user u','p.uid = u.id','LEFT')
            ->join('t_shop s','p.shopid = s.id','LEFT')
            ->join('t_picture pc','s.thumb = pc.id','LEFT')
            ->where("p.uid",$uid)->page($start,$num)->order("addtime desc")->field("p.*,u.username,pc.picurl as thumb,s.name,s.push")->select();
        foreach ($praiseinfo as $key=>$val){
            $pic_array= PictureModel::where("id","in",$praiseinfo[$key]['picture_id'])->field('picurl')->order("addtime desc")->select();
            $praiseinfo[$key]['picture_id']=  $pic_array;
        }
        return $praiseinfo;

    }


}